<?
function drizzo_site_name() {
	$value = drizzo_global_options("site_name");
	echo $value;
}

function drizzo_site_description() {
	return drizzo_global_options("site_description");
}

function drizzo_recent_posts($limit = 5, $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	str_replace(array('<','>'),array('',''),$list_type);
	$the_posts = drizzo_get_the_posts($limit, "0", "publish", "blog,aside");
	$list = "<" . $list_type . " id=\"recent_posts\">\n";
	if($the_posts) {
		foreach($the_posts as $the_post) {
			$list .= $list_item_before . "<a href=\"/blog/" . $the_post['permalink_name'] . "\" title=\"" . $the_post['title'] . "\">" . $the_post['title'] . "</a>" . $list_item_after . "\n";
		}
	}
	$list .= "</" . $list_type . ">\n";
	return $list;
}

function drizzo_recent_comments($limit = 5, $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	global $drizzo_comments;
	str_replace(array('<','>'),'',$list_type);
	$comments_query = @mysql_query("SELECT `ID`,`author_id`,`author_name`,`date`,`post_id` FROM `$drizzo_comments` ORDER BY `date` DESC LIMIT $limit");
	if(@mysql_num_rows($comments_query) > 0) {
		$list = "<" . $list_type . " id=\"recent_posts\">\n";
		$time = drizzo_global_options('time_format');
		while($comment = @mysql_fetch_assoc($comments_query)) {
			$this_post = drizzo_get_the_post($comment['post_id']);
			if($comment['author_id'] && drizzo_user_info($comment['author_id'])) {
				$author_info = drizzo_user_info($comment['author_id']);
				$author = $author_info['nice_name'];
			} else {
				$author = $comment['author_name'];
			}
			$list .= $list_item_before . "By $author on <a href=\"/blog/" . $this_post['permalink_name'] . "#comments\" title=\"" . $this_post['title'] . "\">" . $this_post['title'] . "</a> at " . date($time, $comment['date']) . $list_item_after . "\n";
		}
		$list .= "</" . $list_type . ">\n";
	} else {
		$list = false;
	}
	return $list;
}

function drizzo_theme($option = null) {
	global $drizzo_options;
	
	drizzo_db_connect();
	
	$theme_lookup = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='theme'");
	if(!mysql_errno()) {
		$theme_name = @mysql_fetch_assoc($theme_lookup);
		$theme_name = str_replace(' ','',$theme_name['value']);
	} else {
		$theme_name = "rounded";
	}
	
	if(!$option) {
		return $theme_name;
	}
	
	if(($option == "stylesheet")|| ($option == "css")) {
		$stylesheet = domain . "drizzo-addons/themes/" . $theme_name . "/style.css";
		return $stylesheet;
	}
	
	if($option == "directory") {
		$directory = domain . "drizzo-addons/themes/" . $theme_name . "/";
		return $directory;
	}

	if($option == "local_dir") {
		$directory = root_dir . "/drizzo-addons/themes/" . $theme_name . "/";
		return $directory;
	}
}

function drizzo_startup() {
	if(drizzo_config_file_check()) {
		drizzo_cache();
		ob_start();
		session_start();
		drizzo_update_version_number();
		drizzo_login();
		drizzo_user_info();
		drizzo_get_page_id();
		drizzo_get_page_type();
	}
}

function drizzo_cleanup() {
	drizzo_cache_page();
	ob_flush();
}

function drizzo_head() {
	global $drizzo_options, $drizzo_admin_js;
	echo "<script type=\"text/javascript\" src=\"" . $drizzo_admin_js . "drizzo.js\"></script>\n";
	echo "<script type=\"text/javascript\">setTimezoneOffset()</script>\n";
	echo "<script type=\"text/javascript\" src=\"" . $drizzo_admin_js . "scriptaculous/prototype.js\"></script>\n";
	echo "<script type=\"text/javascript\" src=\"" . $drizzo_admin_js . "scriptaculous/scriptaculous.js\"></script>\n";
	$head_query = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='head_plugins'");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($head_query)) {
			$function = $row['value'];
			if(function_exists($function)) {
				$function();
			}
		}
	}
}

function drizzo_parse_the_uri() {
	global $drizzo_posts;
	$doc_root = root_dir;
	$req_uri = strip_tags(str_replace("?" . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']));
	$scpt = $_SERVER['SCRIPT_FILENAME'];
	$url_array = explode("/",$req_uri);
	array_shift($url_array); //the first one is empty anyway

	if(!empty($url_array)) {
		$final = count($url_array) - 1;
		if(empty($url_array[$final])) {
			$final = count($url_array) - 2;
		}
		
		if($url_array[$final] == 'feed') { // if a feed has been requested
			$drizzo_feed = true;
			if(array_search($url_array[0], array('blog','blogs','page','pages','podcast','podcasts','aside','asides','event','events')) !== false) {
				$drizzo_page_type = $url_array[0];
			}
			if($final > 0) {
				$drizzo_post_id = drizzo_get_post_id($url_array[$final - 1]);
			}
			if($url_array[0] == drizzo_global_options('events_basename')) {
				$drizzo_page_type 	=	"events";
				$drizzo_event_year	=	$url_array[1];
				$drizzo_event_month	=	$url_array[2];
				$drizzo_event_day	=	$url_array[3];
				$drizzo_post_id 	=	drizzo_get_event_id($url_array[4]);
			}
		} else { // if regular content has been requested
			if(array_search($url_array[0], array('blog','blogs','page','pages','podcast','podcasts','aside','asides')) !== false) { // if one of these page types has been specified
				if(array_search($url_array[0], array('podcasts','asides','pages','blogs')) !== false) { // adjust the plural forms
					$drizzo_page_type = rtrim($url_array[0],'s');
				} else {
					$drizzo_page_type = $url_array[0];
				}
				if($url_array[0] == 'page') {
					$drizzo_post_id = drizzo_get_post_id($url_array[$final]);
				} else {
					$drizzo_post_id = drizzo_get_post_id($url_array[1]);
				}
			} elseif($url_array[0] == drizzo_global_options('gallery_basename')) {
				$drizzo_page_type = "gallery";
			} elseif($url_array[0] == drizzo_global_options('events_basename')) {
				$drizzo_page_type 	=	"events";
				$drizzo_event_year	=	$url_array[1];
				$drizzo_event_month	=	$url_array[2];
				$drizzo_event_day	=	$url_array[3];
				$drizzo_post_id 	=	drizzo_get_event_id($url_array[4]);
			} elseif($url_array[0] == "archive") {
				$drizzo_page_type = "archive";
				if($url_array[1] > 0) {
					$drizzo_offset = ($url_array[1] * drizzo_global_options('posts_per_page')) - drizzo_global_options('posts_per_page');
					if($drizzo_offset < 0) {
						$drizzo_offset = 0;
					}
				} else {
					$drizzo_offset = 0;
				}
			} elseif($url_array[0] == 'tag' || $url_array[0] == 'tags') {
				$drizzo_post_id = $url_array[$final];
				$drizzo_page_type = 'tags';
			} elseif(empty($url_array[0])) {
				$drizzo_page_type	=	'index';
				$drizzo_post_id		=	null;
				$drizzo_offset		=	null;
				$drizzo_feed		=	false;
				$drizzo_event_year	=	null;
				$drizzo_event_month	=	null;
				$drizzo_event_day	=	null;
			} else {
				$drizzo_post_id = drizzo_get_post_id($url_array[$final]);
				$post_type_lookup = @mysql_query("SELECT `type` FROM `$drizzo_posts` WHERE `permalink_name`='" . $url_array[$final] . "' LIMIT 1");
				if(!mysql_errno()) {
					$drizzo_page_type = @mysql_result($post_type_lookup, 0);
				}
			}
		}
		$drizzo_uri_vars = array(
			"drizzo_page_type"		=>	$drizzo_page_type,
			"drizzo_post_id"		=>	$drizzo_post_id,
			"drizzo_offset"			=>	$drizzo_offset,
			"drizzo_feed"			=>	$drizzo_feed,
			"drizzo_event_year"		=>	$drizzo_event_year,
			"drizzo_event_month"	=>	$drizzo_event_month,
			"drizzo_event_day"		=>	$drizzo_event_day
			);
		return $drizzo_uri_vars;
	} else {
		return false;
	}
}

function drizzo_feed_list($url, $limit = 10, $id = null, $description = true) {
	if($id == null) {
		$id = str_replace('http://','',$url);
		$id = substr($url, 0, strpos($url,'/',0));
	}
	$feed = new lastRSS;
	$feed->cache_dir = './cache';
	$feed->cache_time = 3600;
	$feed->items_limit = $limit;
	
	$feed_string = "<ul id=\"" . $id . "_rss\">\n";
	if($rs = $feed->get($url)) {
		foreach($rs['items'] as $item) {
			foreach($item as $key => $val) {
				$item[$key] = str_replace(array('<![CDATA[',']]>'),'',$val);
			}
			$feed_string .= "<li class=\"" . $id . "_link\"><a href=\"" . $item['link'] . "\" title=\"" . $item['title'] . "\">" . $item['title'] . "</a>";
			if($description) {
				$feed_string .= "<br />\n<span class=\"rss_desc\">" . $item['description'] . "</span></li>\n";
			}
		}
	} else {
		$feed_string .= "<li>" . $id . " is not available.</li>";
	}
	$feed_string .= "</ul>\n";
	return $feed_string;
}

function drizzo_feed_twitter($user_id, $username = null, $limit = 10) {
	$twitter_url = "http://twitter.com/statuses/user_timeline/$user_id.atom";
	$twitter_rss = curl_init($twitter_url);
	curl_setopt($twitter_rss, CURLOPT_HEADER, 0);
	curl_setopt($twitter_rss, CURLOPT_RETURNTRANSFER, 1);
	$twitter_feed = curl_exec($twitter_rss);
	curl_close($twitter_rss);
	preg_match_all("'<entry(| .*?)>(.*?)</entry>'si", $twitter_feed, $twitts);
	$twitts = $twitts[0];
	$itemtags = array("title","content","id","published","updated","link");
	$count = 1;
	foreach($twitts as $twitt) {
		foreach($itemtags as $itemtag) {
			preg_match("'<$itemtag(| .*?)>(.*?)</$itemtag>'si", $twitt, $rss_item);
			if($rss_item != '') {
				if($itemtag == 'link') {
					$href_start = strpos($twitt,'href=') + 6;
					$href_finish = strpos($twitt, '"', $href_start);
					$href_length = $href_finish - $href_start;
					$href = substr($twitt, $href_start, $href_length);
					$results[$count][$itemtag] = $href;
				} elseif($itemtag == 'content' || $itemtag == 'title') {
					$results[$count][$itemtag] = str_replace($username . ": ", "", $rss_item[2]);
				} elseif($itemtag == 'published' || $itemtag == 'updated') {
					$rss_item[2] = ereg_replace("[^0-9:+-]"," ",$rss_item[2]);
					$rss_item[2] = ereg_replace("[:+-]", " ", $rss_item[2]);
					list($year,$month,$date,$hour,$minutes,$seconds,$offset_hours,$offset_minutes) = split(" ", $rss_item[2]);
					$results[$count][$itemtag] = gmdate("M j, Y \a\\t g:i a", mktime($hour,$minutes,$seconds,$month,$date,$year));
				} else {
					$results[$count][$itemtag] = $rss_item[2];					
				}
			}
		}
		if($count == $limit) {
			break;
		}
		$count++;
	}
	if(count($results) > 0) {
		$twitter = "<ul id=\"twitter_rss\">\n";
		foreach($results as $result) {
			$twitter .= "<li class=\"twitter_link\"><a href=\"" . $result['link'] . "\" title=\"" . $result['title'] . "\">" . $result['content'] . " <small>(" . $result['updated'] . ")</small></a></li>\n";
		}
		$twitter .= "</ul>\n";
	} else {
		$twitter = false;
	}
	return $twitter;
}

function drizzo_feed_flickr($user_id, $limit = 10, $thumbsize = "square") {
	$flickr = new lastRSS;
	$flickr->cache_dir = './cache';
	$flickr->cache_time = 3600;
	$flickr->items_limit = $limit;
	$flickr_url = "http://api.flickr.com/services/feeds/photos_public.gne?id=" . $user_id . "&format=rss2";
	
	$flickr_string = "<ul id=\"flickr_rss\">\n";
	$thumbsizes = array("square","thumb","small","medium","large");
	$thumbsuffixes = array("_s","_t","_m","","_o");
	$thumbsize = str_replace($thumbsizes,$thumbsuffixes,$thumbsize);
	if($rs = $flickr->get($flickr_url)) {
		foreach($rs['items'] as $item) {
			$item['description'] = html_entity_decode($item['description']);
			$start = strpos($item['description'], '<p>', 1);
			$finish = strpos($item['description'], '</p>', $start);
			$length = $finish - $start;
			$item['description'] = substr($item['description'], $start + 3, $length);
			$link = substr($item['description'], 0, (strpos($item['description'],'>')+1));
			$img_start = strpos($item['description'], '<img') + 10;
			$img_finish = strpos($item['description'], '"', $img_start);
			$img_length = $img_finish - $img_start;
			$img_src = str_replace("_m",$thumbsize,substr($item['description'], $img_start, $img_length));
			$img_alt_start = strpos($item['description'], 'alt=') + 5;
			$img_alt_finish = strpos($item['description'], '"', $img_alt_start);
			$img_alt_length = $img_alt_finish - $img_alt_start;
			$img_alt = substr($item['description'], $img_alt_start, $img_alt_length);
			$img_attributes = getimagesize($img_src);
			$flickr_string .= "<li class=\"flickr_link\">" . $link . "<img src=\"" . $img_src . "\" alt=\"" . $img_alt . "\" " . $img_attributes['3'] . " class=\"flickr_image\" /></a></li>\n";
		}
	} else {
		$flickr_string .= "<li>The Flickr feed is not available</li>\n";
	}
	$flickr_string .= "<br class=\"clear\" />\n";
	$flickr_string .= "</ul>\n";
	return $flickr_string;
}

function drizzo_feed_digg($username, $limit = 0) {
	$digg_rss = "http://digg.com/rss/$username/index2.xml";
	$digg_feed = curl_init($digg_rss);
	curl_setopt($digg_feed, CURLOPT_USERAGENT, "Internet Explorer");
	curl_setopt($digg_feed, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($digg_feed, CURLOPT_HEADER, 0);
	$digg = curl_exec($digg_feed);
	curl_close($digg_feed);
	
	$itemtags = array("title","link","description","pubDate","guid","digg:diggCount","digg:username","digg:userimage","digg:category","digg:commentCount");
	preg_match_all("'<item(| .*?)>(.*?)</item>'si", $digg, $feed_items);
	$feed_items = $feed_items[0];
	$count = 1;
	foreach($feed_items as $item) {
		foreach($itemtags as $itemtag) {
			preg_match("'<$itemtag(| .*?)>(.*?)</$itemtag>'si", $item, $rss_item);
			if($rss_item != '') {
				if($itemtag == 'description') {
					$results[$count][$itemtag] = htmlentities(str_replace('’','\'',$rss_item[2]));
				} else {
					$results[$count][$itemtag] = $rss_item[2];
				}
			}
		}
		if($count == $limit) {
			break;
		}
		$count++;
	}
	if(count($results)>0) {
		$digg_string = "<ul id=\"digg_rss\">\n";
		foreach($results as $result) {
			$digg_string .= "<li class=\"digg_link\"><a href=\"" . $result['link'] . "\" title=\"" . $result['title'] . "\">" . $result['title'] . "</a><br class=\"hidden\" />
			<span class=\"rss_desc\">" . $result['description'] . "</span></li>\n";
		}
		$digg_string .= "</ul>\n";
	} else {
		$digg_string = false;
	}
	return $digg_string;
}

function drizzo_feed_lastfm($username, $limit) {
	$lastfm = new lastRSS;
	$lastfm->cache_dir = './cache';
	$lastfm->cache_time = 3600;
	$lastfm->items_limit = $limit;
	$lastfm_url = "http://ws.audioscrobbler.com/1.0/user/" . $username . "/recenttracks.rss";
	$time_offset = -5;
	function is_neg ($num) {
	   return $num < 0;
	}

	function turn_neg ($num) {
	   if (is_neg ($num)) {
	       return $num - $num * 2;
	   } else {
	       return abs ($num);
	   }
	}
	$offset = turn_neg($time_offset);
	if($offset != $time_offset) { $offset_math = "subtract"; }
	if($offset == $time_offset) { $offset_math = "add"; }

	$lastfm_string = "<ul id=\"lastfm_rss\">\n";
	
	if ($rs = $lastfm->get($lastfm_url)) {
		foreach($rs['items'] as $item) {
			$artist_track = preg_split('/ – /', $item['title']);
			$item['artist'] = $artist_track[0];
			$item['track'] = $artist_track[1];
			$timestamp = preg_split('/ /', $item['pubDate']);
			$item['day'] = $timestamp[0];
			if($item['day'] == "Sun,") { $item['day'] = "Sunday"; }
			if($item['day'] == "Mon,") { $item['day'] = "Monday"; }
			if($item['day'] == "Tue,") { $item['day'] = "Tuesday"; }
			if($item['day'] == "Wed,") { $item['day'] = "Wednesday"; }
			if($item['day'] == "Thur,") { $item['day'] = "Thursday"; }
			if($item['day'] == "Fri,") { $item['day'] = "Friday"; }
			if($item['day'] == "Sat,") { $item['day'] = "Saturday"; }
			$item['day_no'] = $timestamp[1];
			$item['month'] = $timestamp[2];
			if($item['month'] == "Jan") { $item['month'] = "January"; }
			if($item['month'] == "Feb") { $item['month'] = "February"; }
			if($item['month'] == "Mar") { $item['month'] = "March"; }
			if($item['month'] == "Apr") { $item['month'] = "April"; }
			if($item['month'] == "May") { $item['month'] = "May"; }
			if($item['month'] == "Jun") { $item['month'] = "June"; }
			if($item['month'] == "Jul") { $item['month'] = "July"; }
			if($item['month'] == "Aug") { $item['month'] = "August"; }
			if($item['month'] == "Sep") { $item['month'] = "September"; }
			if($item['month'] == "Oct") { $item['month'] = "October"; }
			if($item['month'] == "Nov") { $item['month'] = "November"; }
			if($item['month'] == "Dec") { $item['month'] = "December"; }
			$item['year'] = $timestamp[3];
			$item['date'] = $item['day'] . ", " . $item['month'] . " " . $item['day_no'];
			$item['offset'] = $timestamp[5];
			$time = preg_split('/:/', $timestamp[4]);
			$hour = $time[0];
			if($offset_math == "add") { $hour = $hour + ($item_offset + $offset); }
			if($offset_math == "subtract") { $hour = $hour - ($item_offset + $offset); }
			if($hour < 12) { $meridian == "AM"; }
			if($hour == 12) { $meridian == "Noon"; }
			if(($hour == "0") || ($hour == "00")) { $hour = 12; $meridan = "Midnight"; }
			if($hour > 12) { $hour = $hour - 12; $meridian = "PM"; }
			$minute = $time[1];
			$item['time'] = $hour . ":" . $minute . " " . $meridian;
			$lastfm_string .= "<li class=\"lastfm_link\"><a href=\"" . $item['link'] . "\" title=\"" . $item['artist'] . " - " . $item['track'] . "\">" . $item['artist'] . " - " . $item['track'] . "</a><br />
			<span class=\"rss_desc\">Played on " . $item['date'] . " at " . $item['time'] . "</span></li>\n";
		}
	} else {
		$lastfm_string .= "<li>" . $lastfm_url . " is not available.</li>";
	}	
	$lastfm_string .= "</ul>\n";
	return $lastfm_string;
}

function drizzo_is_page($compare = false) {
	global $drizzo_page_type;
	if($compare == $drizzo_page_type || ($compare == 'home' && $drizzo_page_type == 'index')) {
		return true;
	} else {
		return false;
	}
}

function drizzo_title() {
	global $drizzo_post_id, $drizzo_page_type;
	if($drizzo_page_type == 'gallery') {
		$title = drizzo_site_name() . " - " . ucwords(drizzo_global_options('gallery_basename'));
	} elseif($drizzo_page_type == 'events') {
		$title = drizzo_site_name() . " - " . ucwords(drizzo_global_options('events_basename'));
	} elseif($drizzo_page_type == 'tags') {
		$title = drizzo_site_name() . " - " . ucwords(str_replace('-',' ', $drizzo_post_id));
	} elseif(!$drizzo_post_id && strlen(drizzo_site_description()) == 0) {
		$title = drizzo_site_name();
	} elseif(!$drizzo_post_id) {
		$title = drizzo_site_name() . " - " . drizzo_site_description();
	} else {
		$the_post = drizzo_get_the_post($drizzo_post_id);
		$title = drizzo_site_name() . " - " . $the_post['title'];
	}
	return $title;
}

function drizzo_digg_this_link($before = ' | ', $after = ' | ') {
	$size = drizzo_global_options('social_link_digg_size');
	if(drizzo_global_options('social_link_digg') == true) {
		return $before . '<span class="digg_meta"><script type="text/javascript">digg_url = \'' . drizzo_post_permalink() . '\'; digg_skin = \'' . $size . '\';</script><script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></span>' . $after;
	} else {
		return false;
	} 
}

function drizzo_archive_previous_link($link_text = "&#171; Previous", $before = '<span class="previous_link">', $after = '</span>') {
	global $drizzo_posts, $drizzo_offset;
	$offset = $drizzo_offset;
	$limit = drizzo_global_options('posts_per_page');
	$new_offset = $offset + $limit;
	$previous_posts_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `status` IN ('publish') AND `type` IN ('blog','aside') ORDER BY `ID` DESC LIMIT $new_offset,$limit");
	if($offset > 0 && @mysql_num_rows($previous_posts_lookup) > 0) {
		$offset_link = $before . "<a href=\"/archive/". (($offset / $limit) + 2) . "\" title=\"Archive Page " . (($offset / $limit) + 2) . "\">" . $link_text . "</a>" . $after;
	} elseif(@mysql_num_rows($previous_posts_lookup) > 0) {
		$offset_link = $before . "<a href=\"/archive/2\" title=\"Archive Page 2\">" . $link_text . "</a>" . $after;
	} else {
		$offset_link = $before . $after;
	}
	return $offset_link;
}

function drizzo_archive_next_link($link_text = "Next &#187;", $before = '<span class="next_link">', $after = '</span>') {
	global $drizzo_posts, $drizzo_offset;
	$offset = $drizzo_offset;
	$limit = drizzo_global_options('posts_per_page');
	$new_offset = $offset - $limit;
	$new_offset_no = ($offset / $limit);
	if($new_offset_no <= 0) {
		$new_offset_no = 1;
	}
	if($new_offset >= 0) {
		$next_posts_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `status` IN ('publish') AND `type` IN ('blog','aside') ORDER BY `ID` DESC LIMIT $new_offset,$limit");
	}
	if($new_offset >= 1 && @mysql_num_rows($next_posts_lookup) > 0) {
		$offset_link = $before . "<a href=\"/archive/". $new_offset_no . "\" title=\"Archive Page " . $new_offset_no . "\">" . $link_text . "</a>" . $after;
	} elseif(@mysql_num_rows($next_posts_lookup) > 0) {
		$offset_link = $before . "<a href=\"/\" title=\"Home\">" . $link_text . "</a>" . $after;
	} else {
		$offset_link = $before . $after;
	}
	return $offset_link;
}

function drizzo_post_previous_link($link_text = "&#171; ", $before = '<span class="previous_link">', $after = '</span>') {
	global $drizzo_posts, $drizzo_post_id;
	$this_post = drizzo_get_the_post($drizzo_post_id);
	$prev_post_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `status` IN ('publish') AND `type` IN ('blog','aside') AND `posted_date`<'$this_post[posted_date_raw]' ORDER BY `ID` DESC LIMIT 1");
	if(@mysql_num_rows($prev_post_lookup) > 0) {
		$prev_post = @mysql_fetch_assoc($prev_post_lookup);
		$prev_post = drizzo_get_the_post($prev_post['ID']);
		$permalink = drizzo_post_permalink($prev_post['ID']);
		$link = $before . "<a href=\"$permalink\" title=\"$prev_post[title]\">$link_text$prev_post[title]</a>" . $after;
	} else {
		$link = $before . $after;
	}
	return $link;
}

function drizzo_post_next_link($link_text = " &#187; ", $before = '<span class="next_link">', $after = '</span>') {
	global $drizzo_posts, $drizzo_post_id;
	$this_post = drizzo_get_the_post($drizzo_post_id);
	$next_post_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_posts` WHERE `status` IN ('publish') AND `type` IN ('blog','aside') AND `posted_date`>'$this_post[posted_date_raw]' ORDER BY `ID` ASC LIMIT 1");
	if(@mysql_num_rows($next_post_lookup) > 0) {
		$next_post = @mysql_fetch_assoc($next_post_lookup);
		$next_post = drizzo_get_the_post($next_post['ID']);
		$permalink = drizzo_post_permalink($next_post['ID']);
		$link = $before . "<a href=\"$permalink\" title=\"$next_post[title]\">$next_post[title]$link_text</a>" . $after;
	} else {
		$link = $before . $after;
	}
	return $link;
}

function drizzo_prev_and_next($previous_text = "&#171; Previous", $next_text = "Next &#187;", $between_text = null, $previous_before = '<span class="previous_link">', $previous_after = '</span>', $next_before = '<span class="next_link">', $next_after = '</span>') {
	global $drizzo_page_type;
	$url_array = explode("/",$_SERVER['REQUEST_URI']);
	array_shift($url_array);
	$drizzo_page_type = $url_array[0];
	if($drizzo_page_type == 'archive' || $drizzo_page_type == '') {
		$link = drizzo_archive_previous_link($previous_text, $previous_before, $previous_after) . $between_text . drizzo_archive_next_link($next_text, $next_before, $next_after);
	} elseif($drizzo_page_type == 'blog' || $drizzo_page_type == 'aside') {
		$link = drizzo_post_previous_link($previous_text, $previous_before, $previous_after) . $between_text . drizzo_post_next_link($next_text, $next_before, $next_after);
	} else {
		return false;
	}
	return $link;
}
?>
